package com.whq.thrift.api.web.admin.db.consumelog;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

public interface ConsumeLogMapper {

	@Select("select * from t_consume_log_#{tableSeq} where id = #{id}")
	@Results(value = {
			@Result(property = "id", column = "id"), 
			@Result(property = "time", column = "time"), 
			@Result(property = "host", column = "host"),
			@Result(property = "code", column = "invoked_method"), 
			@Result(property = "flow_id", column = "flow_id"),
			@Result(property = "seq", column = "seq"),
			@Result(property = "dir", column = "seq_no"),
			@Result(property = "api", column = "run_method"), 
			@Result(property = "cost", column = "cost"),
			@Result(property = "result", column = "result"),
			@Result(property = "createtTime", column = "create_time") })
	public ConsumeLog getRecordByKey(@Param("tableSeq") int tableSeq, @Param("id") String id);

	@SelectProvider(type = ConsumeLogSqlProvider.class, method = "getSelectRecordsSql")
	@Results(value = {
			@Result(property = "id", column = "id"), 
			@Result(property = "time", column = "time"),
			@Result(property = "host", column = "host"),
			@Result(property = "code", column = "invoked_method"),
			@Result(property = "flow_id", column = "flow_id"), 
			@Result(property = "seq", column = "seq"),
			@Result(property = "dir", column = "seq_no"), 
			@Result(property = "api", column = "run_method"), 
			@Result(property = "cost", column = "cost"),
			@Result(property = "result", column = "result"),
			@Result(property = "createtTime", column = "create_time") })
	public List<ConsumeLog> findRecordsByMap(Map<String, Object> whereParams);

	@SelectProvider(type = ConsumeLogSqlProvider.class, method = "getSelectCountSql")
	public int findRecordsCount(Map<String, Object> whereParams);

	@SelectProvider(type = ConsumeLogSqlProvider.class, method = "getStatByApiCodeSql")
	@Results(value = {
			@Result(property = "api", column = "api"), 
			@Result(property = "result", column = "result"),
			@Result(property = "count", column = "count"), 
			@Result(property = "cost", column = "cost") })
	public List<ConsumeLog> statByApiCode(Map<String, Object> whereParams);

	@SelectProvider(type = ConsumeLogSqlProvider.class, method = "getStatByCodeSql")
	@Results(value = {
			@Result(property = "result", column = "result"),
			@Result(property = "count", column = "count"),
			@Result(property = "cost", column = "cost") })
	public List<ConsumeLog> statByCode(Map<String, Object> whereParams);

	@SelectProvider(type = ConsumeLogSqlProvider.class, method = "getIceCostTimeCountDataSql")
	@Results(value = {
			@Result(property = "host", column = "host"),
			@Result(property = "api", column = "api"), 
			@Result(property = "result", column = "result"),
			@Result(property = "count", column = "count") })
	public List<ConsumeLog> queryIceCostTimeCountData(Map<String, Object> whereParams);

	@Update("REPLACE INTO t_stat_date_api_data "
			+ " (data_date, api, error_code, ip, request_count, create_time) "
			+ " SELECT #{statDay},run_method as api,result,HOST,COUNT(*),CURRENT_TIMESTAMP "
			+ " FROM t_consume_log_#{tableSeq} "
			+ " WHERE time>=#{startTime} AND time<#{endTime} "
			+ " GROUP BY run_method,result,HOST")
	public void replaceStatDateApiData(
			@Param("tableSeq") int tableSeq,
			@Param("statDay") String statDay,
			@Param("startTime") String startTime,
			@Param("endTime") String endTime);
	
	@Update("REPLACE INTO t_stat_hour_api_data (data_date, data_hour, api, error_code, ip, request_count, create_time) " 
			+ " SELECT #{statDay},  #{statHour}, run_method as api, result, HOST, COUNT(*), CURRENT_TIMESTAMP "
			+ " FROM t_consume_log_#{tableSeq} "
			+ " WHERE time>=#{startTime} AND time<#{endTime} "
			+ " GROUP BY run_method,result,HOST")
	public void replaceStatHourApiData(
			@Param("tableSeq") int tableSeq,
			@Param("statDay") String statDay,
			@Param("startTime") String startTime,
			@Param("endTime") String endTime,
			@Param("statHour") int statHour);
	
	@Delete("DELETE FROM t_stat_date_api_data WHERE create_time<#{thresholdTime}")
	public void delDataApiData(@Param("thresholdTime") String thresholdTime);
	
	@Delete("DELETE FROM t_stat_hour_api_data WHERE create_time<#{thresholdTime}")
	public void delHourApiData(@Param("thresholdTime") String thresholdTime);
	
}
